package com.yuncheng.oaApi.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.constant.OaFlowNodeConstant;
import com.yuncheng.oaApi.service.IOaYcSysUserService;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.OaLgqs;
import com.yuncheng.oaApi.service.IOaLgqsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;

/**
 * @Description: 离岗请示审批管理
 * @Author: yuncheng
 * @Date: 2023-06-26
 * @Version: V1.0
 */
@Api(tags = "离岗请示审批管理接口")
@RestController
@RequestMapping("/api/spzx-oa/oaLgqs")
public class OaLgqsController extends BaseController<OaLgqs, IOaLgqsService> {

    private static final Logger log = LoggerFactory.getLogger(OaLgqsController.class);

    @Resource
    private IOaLgqsService oaLgqsService;

    @Resource
    private MyExcelUtils oaMyExcelUtils;

    @Resource
    @Lazy
    private IOaYcSysUserService ycSysUserService;

    /**
     * 分页列表查询
     *
     * @param oaLgqs
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "离岗请示审批管理-分页列表查询")
    @ApiOperation(value = "离岗请示审批管理-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaLgqs oaLgqs,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       HttpServletRequest req) {
        LambdaQueryWrapper<OaLgqs> queryWrapper = new LambdaQueryWrapper<>();

        Page<OaLgqs> page = new Page<>(pageNo, pageSize);
        queryWrapper.like(StringUtils.isNotBlank(oaLgqs.getFxm()), OaLgqs::getFxm, oaLgqs.getFxm());
        queryWrapper.like(StringUtils.isNotBlank(oaLgqs.getFtbbm()), OaLgqs::getFtbbm, oaLgqs.getFtbbm());
        queryWrapper.eq(StringUtils.isNotBlank(oaLgqs.getFlglx()), OaLgqs::getFlglx, oaLgqs.getFlglx());
        if (oaLgqs.getFlgsj() != null) {
            queryWrapper.ge(OaLgqs::getFlgsj, oaLgqs.getFlgsj());
        }
        if (oaLgqs.getFhgsj() != null) {
            queryWrapper.le(OaLgqs::getFhgsj, oaLgqs.getFhgsj());
        }
        queryWrapper.eq(StringUtils.isNotBlank(oaLgqs.getFlczt()), OaLgqs::getFlczt, oaLgqs.getFlczt());
        queryWrapper.orderByDesc(OaLgqs::getFlgsj);
        IPage<OaLgqs> pageList = oaLgqsService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }


    /**
     * 编辑
     *
     * @param oaLgqs
     * @return
     */
    @AutoLog(value = "离岗请示审批管理-编辑")
    @ApiOperation(value = "离岗请示审批管理-编辑")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaLgqs oaLgqs) {
        oaLgqsService.updateById(oaLgqs);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }
//
//    /**
//     * 通过id删除
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "离岗请示审批管理-通过id删除")
//    @ApiOperation(value = "离岗请示审批管理-通过id删除")
//    @DeleteMapping(value = "/delete")
//    public HttpResult<?> delete(@RequestParam("id") String id) {
//        oaLgqsService.removeById(id);
//        return HttpResult.ok(I18nUtil.message("delete.success"));
//    }
//
//    /**
//     * 通过id批量删除
//     *
//     * @param ids
//     * @return
//     */
//    @AutoLog(value = "离岗请示审批管理-批量删除")
//    @ApiOperation(value = "离岗请示审批管理-批量删除")
//    @DeleteMapping(value = "/deleteBatch")
//    public HttpResult<?> deleteBatch(@RequestParam("ids") String ids) {
//        oaLgqsService.removeByIds(Arrays.asList(ids.split(",")));
//        return HttpResult.ok(I18nUtil.message("delete.success"));
//    }
//
//    /**
//     * 通过id查询
//     *
//     * @param id
//     * @return
//     */
//    @AutoLog(value = "离岗请示审批管理-通过id查询")
//    @ApiOperation(value = "离岗请示审批管理-通过id查询")
//    @GetMapping(value = "/queryById")
//    public HttpResult<?> queryById(@RequestParam("id") String id) {
//        OaLgqs oaLgqs = oaLgqsService.getById(id);
//        return HttpResult.ok(oaLgqs);
//    }


    /**
     * 添加
     *
     * @param oaLgqs
     * @return
     */
    @AutoLog(value = "流程业务主表-添加")
    @ApiOperation(value = "流程业务主表-添加")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody OaLgqs oaLgqs) {
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        if (StringUtils.isBlank(oaLgqs.getId())){
            oaLgqs.setId(GetUuIdUtils.ReplaceUuId());
        }
        if (StringUtils.isBlank(oaLgqs.getFxm())){
            oaLgqs.setFxm(currentUser.getName());
        }
        if (StringUtils.isBlank(oaLgqs.getFtbbm())){
            oaLgqs.setFtbbm(currentUser.getDeptName());
        }
        if (StringUtils.isBlank(oaLgqs.getFzw())){
            oaLgqs.setFzw(ycSysUserService.getById(currentUser.getId()).getFzw());
        }
        if (StringUtils.isBlank(oaLgqs.getFuserid())){
            oaLgqs.setFuserid(currentUser.getId());
        }

        oaLgqsService.save(oaLgqs);


        return HttpResult.ok(oaLgqs);
    }
    /**
     * 数据转换
     * @param oaLgqs
     */
    private void formatValue(OaLgqs oaLgqs){
    }




    @AutoLog(value = "导出离岗请示记录数据-表格excel")
    @ApiOperation(value = "导出离岗请示记录数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "主键id字符逗号分割",required = true)
    })
    @GetMapping("/exportLgqsData")
    public void exportLgqsData(@RequestParam(value = "ids",defaultValue = "")String ids,
                               OaLgqs ndkpBasic,
                               HttpServletResponse response){
        try {
            String name = "exportLgqsData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出
            if (StringUtils.isNotEmpty(ids)) {
                List<OaLgqs> ndkpBasicList = oaLgqsService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaLgqs e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }else {
                LambdaQueryWrapper<OaLgqs> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(StringUtils.isNotBlank(ndkpBasic.getFuserid()),OaLgqs::getFuserid,ndkpBasic.getFuserid());
                //填报部门
                queryWrapper.eq(StringUtils.isNotBlank(ndkpBasic.getFtbbm()),OaLgqs::getFtbbm, ndkpBasic.getFtbbm());
                //姓名
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFxm()),OaLgqs::getFxm,ndkpBasic.getFxm());
                //职务
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFzw()),OaLgqs::getFzw,ndkpBasic.getFzw());
                //离岗类型
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFlglx()),OaLgqs::getFlglx,ndkpBasic.getFlglx());
                //报送时间
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFbssj()),OaLgqs::getFbssj,ndkpBasic.getFbssj());
                if (StringUtils.isBlank(ndkpBasic.getFlczt())) {

                }else if (ndkpBasic.getFlczt().equals(OaFlowNodeConstant.HJMC_LCJS)){
                    queryWrapper.like(StringUtils.isNotBlank(ndkpBasic.getFlczt()), OaLgqs::getFlczt, OaFlowNodeConstant.HJMC_LCJS);
                }else {
                    queryWrapper.isNull(OaLgqs::getFlczt);
                }
                if (ndkpBasic.getFlgsj() != null){
                    queryWrapper.ge(OaLgqs::getFlgsj, ndkpBasic.getFlgsj());
                }
                if (ndkpBasic.getFhgsj() != null){
                    queryWrapper.le(OaLgqs::getFhgsj, ndkpBasic.getFhgsj());
                }
                List<OaLgqs> ndkpBasicList = oaLgqsService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaLgqs e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }
            oaMyExcelUtils.outputListToExclFile(OaBusinessConstant.IMPORT_OA_LGQS_XML,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出离岗请示信息数据出错-{}",e.getMessage());
        }
    }
}
